﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace XMM.Objects
{
    [Serializable()]
    public class VideoInfo : Object
    {
        public int VideoTracks { get; set; }

        public string Codec { get; set; }

        public int HDLevel { get; set; }

        public List<string> SubtitleLanguages { get; set; }

        public int SubtitleTracks
        {
            get
            {
                return SubtitleLanguages.Count;
            }
        }

        public VideoInfo()
        {
            blankValues();
        }

        public void blankValues()
        {
            VideoTracks = 0;
            Codec = String.Empty;
            HDLevel = 0;
            SubtitleLanguages = new List<string>();
        }

        public System.Data.Linq.Binary ToBinary()
        {
            System.Data.Linq.Binary binVideoInfo = new System.Data.Linq.Binary(null);

            try
            {
                MemoryStream oSerializeStream = new MemoryStream();
                BinaryFormatter oSerializer = new BinaryFormatter();
                oSerializer.Serialize(oSerializeStream, this);
                oSerializeStream.Flush();

                List<byte> lReadBytes = new List<byte>();
                byte[] baBuffer = new byte[4096];
                decimal lTotalBlocks = oSerializeStream.Length / baBuffer.LongLength;
                double dTotalBlocks = Math.Ceiling((double)lTotalBlocks);
                int iTotalBlocks = (int)dTotalBlocks;

                for (int iCurrentBlock = 1; (iCurrentBlock - 1) <= iTotalBlocks; iCurrentBlock++)
                {
                    //baBuffer = new byte[4096];
                    int iMaxReadBytes = int.Parse((oSerializeStream.Length - ((iCurrentBlock - 1) * 4096)).ToString());
                    if (iMaxReadBytes > 4096)
                        iMaxReadBytes = 4096;

                    baBuffer = new byte[iMaxReadBytes];

                    oSerializeStream.Read(baBuffer, ((iCurrentBlock - 1) * 4096), iMaxReadBytes);
                    byte[] baStreamBuffer = oSerializeStream.GetBuffer();

                    for (int iCurrentByte = 0; iCurrentByte < baBuffer.Length; iCurrentByte++)
                    {
                        baBuffer[iCurrentByte] = baStreamBuffer[iCurrentByte];
                    }

                    foreach (byte bData in baBuffer)
                    {
                        lReadBytes.Add(bData);
                    }
                }

                oSerializeStream.Close();
                binVideoInfo = new System.Data.Linq.Binary(lReadBytes.ToArray());
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Fehler beim Konvertieren der Audio-Informationen für die Datenbank: " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }

            return binVideoInfo;
        }

        public bool buildFromBinary(System.Data.Linq.Binary binAudioInfo)
        {
            try
            {
                MemoryStream oDeserializeStream = new MemoryStream();
                byte[] baSerializedData = binAudioInfo.ToArray();
                byte[] baBuffer = new byte[4096];
                long lTotalBlocks = baSerializedData.LongLength / baBuffer.LongLength;
                double dTotalBlocks = Math.Ceiling((double)lTotalBlocks);
                int iTotalBlocks = (int)dTotalBlocks;

                for (int iCurrentBlock = 1; (iCurrentBlock - 1) <= iTotalBlocks; iCurrentBlock++)
                {
                    baBuffer = new byte[4096];
                    if (baSerializedData.Length < 4096)
                        baBuffer = new byte[baSerializedData.Length];

                    int iBufferPosition = 0;

                    for (int iCurrentPosition = (iCurrentBlock - 1) * 4096; iCurrentPosition <= (baSerializedData.Length - 1) && iCurrentPosition < 4097; iCurrentPosition++)
                    {
                        if (iBufferPosition == 4095)
                        {
                            break;
                        }

                        baBuffer[iBufferPosition] = baSerializedData[iCurrentPosition];
                        ++iBufferPosition;
                    }

                    oDeserializeStream.Write(baBuffer, ((iCurrentBlock - 1) * 4096), baBuffer.Length);
                    oDeserializeStream.Flush();
                }

                oDeserializeStream.Position = 0;
                BinaryFormatter oDeserializer = new BinaryFormatter();
                VideoInfo oDeserializedInfo = (Objects.VideoInfo)oDeserializer.Deserialize(oDeserializeStream);
                oDeserializeStream.Close();

                this.Codec = oDeserializedInfo.Codec;
                this.HDLevel = oDeserializedInfo.HDLevel;
                this.SubtitleLanguages = oDeserializedInfo.SubtitleLanguages;
                this.VideoTracks = oDeserializedInfo.VideoTracks;

                return true;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show("Fehler beim Abrufen der Audio-Infos aus der Datenbank: " + ex.Message, "Fehler", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }

            blankValues();
            return false;
        }
    }
}
